package com.woniuxy.hrp.mapper;

import com.woniuxy.hrp.entity.Patient;
import com.woniuxy.hrp.entity.Reservation;
import com.woniuxy.hrp.provider.PatientMapperSqlProvider;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/**
 * @program: hrph
 * @InterfaceName PatientMapper
 * @description:患者DAO
 * @author: LRW
 * @create: 2022-08-28 00:38
 * @Version 1.0
 *
 * --ffs 增加code 0904
 **/
@Mapper
public interface PatientMapper {

    /***
     * @description: 增：患者注册
     * @param: patient
     * @return: void
     * @author Lrw
     * @date: 2022/8/28 0:59
     */
    @Insert("insert into hrp_patient(account,password,status,email,phone,address,idCard,mlcardnumber) values(#{account},#{password},#{status},#{email},#{phone},#{address},#{idCard},#{mlCardNumber})")
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    void add(Patient patient);

    /*** 
     * @description: 改：根据id锁定患者帐号
     * @param: id 
     * @return: void 
     * @author Lrw
     * @date: 2022/8/28 1:13
     */
    @Update("update hrp_patient set status='n' where id=#{id}")
    void delete(int id);

    /***
     * @description: 改：指定患者身份认证
     * @param: id
     * @return: void
     * @author Lrw
     * @date: 2022/8/28 1:17
     */
    @Update("update hrp_patient set status='y' where id=#{id}")
    void authById(int id);

    /***
     * @description: 改：动态修改指定id的患者信息(帐号、就诊卡编号、状态不能修改）
     * @param: patient
     * @return: void
     * @author Lrw
     * @date: 2022/8/28 14:10
     */
    @UpdateProvider(type = PatientMapperSqlProvider.class, method = "updatePatient")
    void update(Patient patient);

    /*** 
     * @description: 查：按id查找患者
     * @param: id 
     * @return: com.woniuxy.hrp.entity.Patient
     * @author Lrw
     * @date: 2022/8/28 1:29
     */
    @Select("select * from hrp_patient where id=#{id}")
    @Results(id = "patientMap",value = {
            @Result(column = "id",property = "id",id = true),
            @Result(column = "id",property = "orderUsers",many = @Many(select = "com.woniuxy.hrp.mapper.OrderUserMapper.selectByPatientId")),
    })
    Patient selectById(int id);
    
    /***
     * @description: 查：多条件查询患者数据（集合）
     * @param: patient
     * @return: 符合查询条件的患者集合
     * @author Lrw
     * @date: 2022/8/28 1:20
     */
    @SelectProvider(type = PatientMapperSqlProvider.class, method = "selectPatientByCondition")
    List<Patient> selectByCondition(Patient patient);


    /***
     * @description: 查：根据患者帐号和密码查询（登录）,且根据status=Y（已成功激活）
     * @param: patient
     * @return: null代表账号或密码错误；patient代表帐号密码正确
     * @author Lrw
     * @date: 2022/8/28 13:59
     */
    @Select("select * from hrp_patient where account=#{account} and password=#{password} and status = 'Y' ")
    @ResultMap(value = "patientMap")
    Patient selectByAccAndPsw(Patient patient);

    /***
     * @description: 查：患者列表
     * @param:
     * @return: 所有患者数据集合
     * @author Lrw
     * @date: 2022/8/28 14:13
     */
    @Select("select * from hrp_patient")
    List<Patient> selectAll();

    /**
     * 修改密码
     * @param patient
     */
    @Update("update hrp_patient set password=#{password} where id=#{id}")
    void updatePassWord(Patient patient);

    /**
     * 账号、邮箱、手机号不能重复
     * @param patient
     * @author shanshanfan
     * @return
     */
    @SelectProvider(type = PatientMapperSqlProvider.class, method = "getNumsByAccOrEmailOrPhone")
    int getNumsByAccOrEmailOrPhone(Patient patient);

//    /**
//     * 根据激活码code查询用户
//     * 状态：U-未激活邮箱，默认状态 Y-激活成功 F-冻结
//     *
//     * @param code
//     * @author shanshanfan
//     * @return Patient
//     */
//    @Select("select * from hrp_patient where code =#{code}")
//    Patient checkCode(String code);

    /**
     * 激活账户，修改患者账户状态，从U-> Y
     * 状态：U-未激活邮箱，默认状态 Y-激活成功 F-冻结
     *
     * @param patient
     * @author shanshanfan
     * @return
     */
    @Update("update hrp_patient set status='Y' where id=#{id}")
    void updatePatientStatus(Patient patient);

    /**
     * 查询预约信息（按条件查询） 连预约信息表
     * 预约信息表中状态：T-待完成的预约 D-已完成预约 U-未付款的预约
     *
     * @param status
     * @author shanshanfan
     * @return List<Reservation>
     */
    @Select("select hp.name, hr.* from hrp_patient hp, hrp_reservation hr where hp.id = hr.patient_id and status =#{status}")
    List<Reservation> getReservationByStatus(String status);
    /**
     * desc: 根据患者账号查询信息
     * @Param: [username]
     * @Return: [java.lang.String]
     * @Author: caojieke
     * @Date: 2022/9/5 20:28
     */
    @Select("select * from hrp_patient where account =#{username}")
    Patient getByAccount(String username);

    @Select("select * from hrp_patient where id =#{id}")
    Patient getByid(String id);

    @SelectProvider(type = PatientMapperSqlProvider.class, method = "updatePatient")
    void updateByCondition(Patient patient);
}
